﻿using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;

namespace LanqiQAwebapi.Models
{
    public partial class LanQiQaDBContext : DbContext
    {
        public LanQiQaDBContext()
        {
        }

        public LanQiQaDBContext(DbContextOptions<LanQiQaDBContext> options)
            : base(options)
        {
        }

       
        public virtual DbSet<Comment> Comments { get; set; } = null!;
        public virtual DbSet<Follow> Follows { get; set; } = null!;
    
        public virtual DbSet<Qa> Qas { get; set; } = null!;
        public virtual DbSet<Question> Questions { get; set; } = null!;
        public virtual DbSet<User> Users { get; set; } = null!;

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
               base.OnConfiguring(optionsBuilder);
            }
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
       

            modelBuilder.Entity<Comment>(entity =>
            {
                entity.ToTable("Comment");

                entity.Property(e => e.CommentId).HasColumnName("CommentID");

                entity.Property(e => e.AnswerId).HasColumnName("AnswerID");

                entity.Property(e => e.Comment1)
                    .HasMaxLength(100)
                    .IsUnicode(false)
                    .HasColumnName("Comment");

                entity.Property(e => e.UserId).HasColumnName("UserID");

                entity.HasOne(d => d.Answer)
                    .WithMany(p => p.Comments)
                    .HasForeignKey(d => d.AnswerId)
                    .HasConstraintName("FK__Comment__AnswerI__29572725");

                entity.HasOne(d => d.User)
                    .WithMany(p => p.Comments)
                    .HasForeignKey(d => d.UserId)
                    .HasConstraintName("FK__Comment__UserID__2A4B4B5E");
            });

            modelBuilder.Entity<Follow>(entity =>
            {
                entity.ToTable("Follow");

                entity.Property(e => e.FollowId).HasColumnName("FollowID");

                entity.Property(e => e.FollowedUserId).HasColumnName("FollowedUserID");

                entity.Property(e => e.UserId).HasColumnName("UserID");

                entity.HasOne(d => d.FollowedUser)
                    .WithMany(p => p.FollowFollowedUsers)
                    .HasForeignKey(d => d.FollowedUserId)
                    .HasConstraintName("FK__Follow__Followed__37A5467C");

                entity.HasOne(d => d.User)
                    .WithMany(p => p.FollowUsers)
                    .HasForeignKey(d => d.UserId)
                    .HasConstraintName("FK__Follow__UserID__36B12243");
            });

       

            modelBuilder.Entity<Qa>(entity =>
            {
                entity.ToTable("Qa");

                entity.Property(e => e.QaId).HasColumnName("QaID");

                entity.Property(e => e.AvatarPath)
                    .HasMaxLength(100)
                    .IsUnicode(false);

                entity.Property(e => e.Question)
                    .HasMaxLength(100)
                    .IsUnicode(false);

                entity.Property(e => e.TextFilePath)
                    .HasMaxLength(100)
                    .IsUnicode(false);

                entity.Property(e => e.UserId).HasColumnName("UserID");

                entity.HasOne(d => d.User)
                    .WithMany(p => p.Qas)
                    .HasForeignKey(d => d.UserId)
                    .HasConstraintName("FK__Qa__UserID__267ABA7A");
            });

            modelBuilder.Entity<Question>(entity =>
            {
                entity.ToTable("Question");

                entity.Property(e => e.QuestionId).HasColumnName("QuestionID");

                entity.Property(e => e.QaId).HasColumnName("QaID");

                entity.Property(e => e.Question1)
                    .HasMaxLength(100)
                    .IsUnicode(false)
                    .HasColumnName("Question");

                entity.Property(e => e.UserId).HasColumnName("UserID");

                entity.HasOne(d => d.Qa)
                    .WithMany(p => p.Questions)
                    .HasForeignKey(d => d.QaId)
                    .OnDelete(DeleteBehavior.SetNull)
                    .HasConstraintName("FK__Question__QaID__2D27B809");

                entity.HasOne(d => d.User)
                    .WithMany(p => p.Questions)
                    .HasForeignKey(d => d.UserId)
                    .HasConstraintName("FK__Question__UserID__2E1BDC42");
            });

            modelBuilder.Entity<User>(entity =>
            {
                entity.ToTable("User");

                entity.Property(e => e.UserId).HasColumnName("UserID");

                entity.Property(e => e.AvatarPath)
                    .HasMaxLength(100)
                    .IsUnicode(false);

                entity.Property(e => e.Password)
                    .HasMaxLength(50)
                    .IsUnicode(false);

                entity.Property(e => e.Phone)
                    .HasMaxLength(20)
                    .IsUnicode(false);

                entity.Property(e => e.UserName)
                    .HasMaxLength(50)
                    .IsUnicode(false);
            });

            OnModelCreatingPartial(modelBuilder);
        }

        partial void OnModelCreatingPartial(ModelBuilder modelBuilder);
    }
}
